package com.icss.clothes.mapper;

import com.icss.clothes.entity.Order_detail;
import com.icss.clothes.entity.Orders;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMapper {
    //从数据库中通过指定的订单编号查询得到所有订单信息
    @Select("select * from orders where order_id=#{order_id}")
    public Orders selectAllByOrder_id(String order_id);
    //向订单表中插入一条指定的订单信息
    @Insert("insert into orders(order_id,user_id,order_price,order_state,order_time)" +
            " values(#{order_id},#{user_id},#{order_price},#{order_state},now())")
    public void insertIntoByOrders(Orders orders);
    //向订单详情表中插入一条指定的订单详情信息
    @Insert("insert into order_detail(order_id,c_id,cd_id,od_number)" +
            " values(#{order_id},#{c_id},#{cd_id},#{od_number})")
    public void insertIntoByOrder_detail(Order_detail orderDetail);
    //从数据库中通过指定的用户编号查询得到所有订单信息
    @Select("select * from orders where user_id=#{user_id}")
    public List<Orders> selectAllByUser_id(int user_id);
    //从数据库中通过指定的订单编号查询得到商品详情信息
    @Select("select cr.color_name,s.size_name,c.c_name,od.*,cd.* from order_detail od join commodity_detail cd on od.cd_id=cd.cd_id" +
            " join commodity c on od.c_id=c.c_id join color cr on cr.color_id=cd.color_id join size s on s.size_id=cd.size_id" +
            " where order_id=#{order_id}")
    public List<Order_detail> selectOrderDetailByOrder_id(String order_id);
    //通过指定的订单编号删除订单表中的订单信息
    @Delete("delete from orders where order_id=#{order_id}")
    public void deleteOrderByOrder_id(String order_id);
    //通过指定的订单编号删除订单详情表中的订单信息
    @Delete("delete from order_detail where order_id=#{order_id}")
    public void deleteOrderDetailByOrder_id(String order_id);
    @Insert("update orders set adress_id=#{address_id} where order_id=#{order_id}")
    public void updateAddressByOrder_id(@Param("order_id") String order_id,
                                          @Param("address_id") int address_id);
    //通过指定的订单编号修改指定的订单状态
    @Update("update orders set order_state=#{order_state} where order_id=#{order_id}")
    public void updateStateByOrder_id(@Param("order_id") String order_id,
                                      @Param("order_state") int order_state);

    //通过用户id和商品id判断用户是否购买过该商品
    @Select("select * from orders o left outer join order_detail od" +
            " on o.order_id=od.order_id where user_id=#{user_id} and c_id=#{c_id} ")
    public Orders selectByUidAndCid(@Param("user_id") int user_id, @Param("c_id") int c_id);
}
